package com.dehui.traders.ui.optioanl.adapter;

import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.bean.OptionalCode;
import com.dehui.traders.utils.BaseRecyclerAdapter;
import com.dehui.traders.utils.SmartViewHolder;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.Collection;
import java.util.List;

/**
 * Created by hqq on 18.4.19.
 */

public class OptionalStockManagerAdapter extends BaseRecyclerAdapter<OptionalCode>{

    private SwipeMenuRecyclerView recyclerView;
    private OnItemClick onItemClick;

    public OptionalStockManagerAdapter(Collection<OptionalCode> collection, int layoutId, SwipeMenuRecyclerView recyclerView) {
        super(collection, layoutId);
        this.recyclerView = recyclerView;
    }

    @Override
    protected void onBindViewHolder(SmartViewHolder holder, OptionalCode model, int position) {
        TextView tvTitle = holder.findViewById(R.id.tv_title);
        TextView tvCode = holder.findViewById(R.id.tv_code);
        ImageView ivStick = holder.findViewById(R.id.iv_stick);
        ImageView ivRemind = holder.findViewById(R.id.iv_remind);
        ImageView ivSelect = holder.findViewById(R.id.iv_select);
        tvTitle.setText(model.getName());
        tvCode.setText(model.getCode());
        holder.findViewById(R.id.iv_drag).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action){
                    case MotionEvent.ACTION_DOWN:
                        recyclerView.startDrag(holder);
                        break;
                }
                return false;
            }
        });

        ivStick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null)
                    onItemClick.onStick(position,mList);
            }
        });

        ivRemind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClick != null)
                    onItemClick.onRemind(position);
            }
        });

        ivSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model.setSelect(!model.isSelect());
                if (onItemClick != null)
                    onItemClick.onSelect();
                OptionalStockManagerAdapter.this.notifyDataSetChanged();
            }
        });

        int status = model.getStatus();
        if (status == 0)
            ivRemind.setImageResource(R.drawable.alarm_clock);
        else
            ivRemind.setImageResource(R.drawable.alarm_clock_red);

        ivSelect.setSelected(model.isSelect());

    }

    public interface OnItemClick{
        void onStick(int pos, List<OptionalCode> mList);
        void onRemind(int pos);
        void onSelect();
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}
